import { defineStore } from 'pinia'

export const useUserStore = defineStore('user', {
  state: () => ({
    isLoggedIn: false,
    username: sessionStorage.getItem('username') || '',
    password: sessionStorage.getItem('password') || '',
    token: ''
  }),
  actions: {
    login(username, password) {
      this.isLoggedIn = true
      this.username = username
      this.password = password
      this.token = new Date().getTime().toString()
      sessionStorage.setItem('username', username)
      sessionStorage.setItem('password', password)
    },
    logout() {
      this.isLoggedIn = false
      this.username = ''
      this.password = ''
      this.token = ''
      sessionStorage.removeItem('username')
      sessionStorage.removeItem('password')
    }
  },
  persist: {
    enabled: true,
    strategies: [
      {
        key: 'user',
        storage: sessionStorage
      }
    ]
  }
})